public with sharing class myAutoController {
	
	public Map<Id,Auto__c> mapAuto{get;set;}
	public List<Auto__c> listAuto{get;set;}
	public ApexPages.StandardController ctrl;
	public String selectedEntry{get;set;}
	
	/*
	public/private | Rückgabewert | Methodennamen (Parameter){
		Vodoo
	}
	
	public decimal getExponent(decimal input){
		return Math.getExponent(input);
	}
	*/
	
	//Constructor
	public myAutoController(ApexPages.StandardController stCtrl){
		ctrl = stCtrl;
		
		mapAuto = new Map<Id,Auto__c>([
								SELECT Name,Marke__c,Modell__c,Preis__c FROM Auto__c
		]);
		
		listAuto = mapAuto.values();
	}//END myAutoController()
	
	public PageReference save(){
		Auto__c myPageValues = (Auto__c)ctrl.getRecord();
		
		Auto__c auto2Insert = new Auto__c(
			Name = myPageValues.Name,
			Marke__c = myPageValues.Marke__c,
			Modell__c = myPageValues.Modell__c,
			Preis__c = myPageValues.Preis__c
			
		);
		
		insert auto2Insert;
		
		PageReference currentPage = new PageReference('/apex/myAutoPage');
		currentPage.setRedirect(true);
		return currentPage;
		
	}
	
	public List<SelectOption> getEntries(){
		List<SelectOption> results = new List<SelectOption>();
		
		for(Auto__c a: mapAuto.values()){
			results.add(new SelectOption(a.Id,a.Name+' '+a.Marke__c+' '+a.Modell__c+' '+a.Preis__c));
		}
		
		results.sort();
		
		return results;
	}
	
	public void editEntry(){
		
		
		Auto__c autoSelectedEntry = mapAuto.get(selectedEntry);
		Auto__c currentPageValues = (Auto__c)ctrl.getRecord();
		
		currentPageValues.Name = autoSelectedEntry.Name;
		currentPageValues.Marke__c  = autoSelectedEntry.Marke__c;
		currentPageValues.Modell__c = autoSelectedEntry.Modell__c;
		currentPageValues.Preis__c = autoSelectedEntry.Preis__c;
		
		
	}
	
	public PageReference updateEntry(){
		Auto__c selectedAutoEntry = mapAuto.get(selectedEntry);
		Auto__c pageValues = (Auto__c) ctrl.getRecord();
		
		selectedAutoEntry.Name = pageValues.Name;
		selectedAutoEntry.Marke__c = pageValues.Marke__c;
		selectedAutoEntry.Modell__c = pageValues.Modell__c;
		selectedAutoEntry.Preis__c = pageValues.Preis__c;
		
		update selectedAutoEntry;
		
		PageReference currentPage = new PageReference('/apex/myAutoPage');

        currentPage.setRedirect(true);
        return currentPage;
	}
	
	public PageReference deleteEntry(){

		delete mapAuto.get(selectedEntry);
		
		PageReference currentPage = new PageReference('/apex/myAutoPage');

        currentPage.setRedirect(true);
        return currentPage;
	}
	
	public static testmethod void myUnitTest(){
		Auto__c testAuto = new Auto__c(
			Name='TestAuto',
			Marke__c = 'TestMarke',
			Modell__c = 'TestModell',
			Preis__c = 100.00
		);
		
		insert testAuto;
		
		ApexPages.StandardController controller = new ApexPages.StandardController(testAuto);
		
		myAutoController myPageCtrl = new myAutoController(controller);
		
		myPageCtrl.getEntries();
		
		myPageCtrl.selectedEntry = testAuto.Id;
		
		myPageCtrl.editEntry();
		
		myPageCtrl.updateEntry();
		
		myPageCtrl.save();
		
		myPageCtrl.deleteEntry();
		
	}

}